<?php
/**
 * 结算控制器
 */
namespace Admin\Controller;
use Think\Controller;

class BillController extends AdminController
{
    public function _initialize()
    {
        parent::_initialize();
    }
    
    /**
     * 列表页
     */
    public function index()
    {
        $REQUEST = I('request.');
        $map = $order = array();
        
        $date_start = strtotime($REQUEST['date_start']);
        $date_end = strtotime($REQUEST['date_end']);
        //时间筛选
        if($date_start && $date_end){
            $map['mt_bill.ctime'] = array('BETWEEN', array($date_start, $date_end));
        } else if($date_start){
            $map['mt_bill.ctime'] = array('EGT', $date_start);
        } else if($date_end){
            $map['mt_bill.ctime'] = array('ELT', $date_end);
        }
        
        //类型筛选
        if ($REQUEST['status'] == 'done') {
            $search['status'] = 'done';
            $map['mt_bill.status'] = 1;
        } else if($REQUEST['status'] == 'doing') {
            $search['status'] = 'doing';
            $map['mt_bill.status'] = 0;
        }
        
        //关键词搜索
        if(!empty($REQUEST['name'])){
            $map['mt_cinema_host.cinema_name'] = array('LIKE', '%'.$REQUEST['name'].'%');
        }
        
        //查询总数
        $count = M('bill')->field('mt_bill.*, mt_cinema_host.cinema_name cinema_name')
        ->join('left join mt_cinema_host on mt_bill.host_name=mt_cinema_host.host_name')
        ->where($map)->count();
        
        //构造分页
        $listRows = isset($REQUEST['r']) ? (int)$REQUEST['r'] : C('LIST_ROWS');
        if(ceil($count/$listRows) < floatval($REQUEST['p'])){
            $_GET['p'] = ceil($count/$listRows);
        }
        $page = new \Think\Page2($count, $listRows, $REQUEST);
        $data['pager'] =$page->show();
        $limit = $page->firstRow.','.$page->listRows;
        
        //结算总金额
        $total_money = M('bill')->join('left join mt_cinema_host on mt_bill.host_name=mt_cinema_host.host_name')
        ->where($map)->sum('mt_bill.money_total');

        if($REQUEST['baobiao']==1){//下载报表
            //查询数据列表数据
            $data['data_list'] = M('bill')->field('mt_bill.*, mt_cinema_host.cinema_name cinema_name')
                ->join('left join mt_cinema_host on mt_bill.host_name=mt_cinema_host.host_name')
                ->where($map)->order("mt_bill.id DESC")->select();

            foreach($data['data_list'] as $k=>$v){
                $data['data_list'][$k]['ctime'] = date('Y-m-d',$v['ctime']);
                $data['data_list'][$k]['cinema_name'] = $v['cinema_name'];
                $data['data_list'][$k]['money_total'] = $v['money_total'];
                $data['data_list'][$k]['status'] = $v['status']?'已结算':'未结算';
            }

            $xlsName  = "影院运营平台结算管理数据报表";
            $xlsCell  = array(
                array('bill_sn','结算单号'),
                array('ctime','结算时间'),
                array('cinema_name','结算影院'),
                array('money_total','结算金额(元)'),
                array('status','结算状态'),
            );
            $array=array($total_money,$count);
            $this->exportExcel($xlsName,$xlsCell, $data['data_list'],$array);
        }else{
            //查询数据列表数据
            $data['data_list'] = M('bill')->field('mt_bill.*, mt_cinema_host.cinema_name cinema_name')
                ->join('left join mt_cinema_host on mt_bill.host_name=mt_cinema_host.host_name')
                ->where($map)->order("mt_bill.id DESC")->limit($limit)->select();
        }
        $this->assign('search', $search);
        $this->assign($data);
        $this->assign('total_count', $count);
        $this->assign('total_money', $total_money);
        $this->display();
    }
    /**
     * @desc  报表导出 @copy.he
     * @param $expTitle
     * @param $expCellName
     * @param $expTableData
     */
    public function exportExcel($expTitle,$expCellName,$expTableData,$array=''){
        $xlsTitle = iconv('utf-8', 'gbk', $expTitle);//文件名称
        $fileName = $xlsTitle.date('_Ymd');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        // vendor("PHPExcel.PHPExcel");
        include ROOT_PATH.'\Thinkphp\Library\Vendor\PHPExcel\PHPExcel.php';
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        $num = $array[1]+4;
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$num, "结算总金额:".$array[0].'元，结算次数:'.$array[1].'次' );
        ob_end_clean();
        header('pragma:public');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
    /**
     * 详情页
     */
    public function detail()
    {
        $bill_sn = I('request.bill_sn');
        $data['bill'] = D("Common/Bill")->getDetail($bill_sn);
        $data['bill']['pay_money'] = $data['bill']['money_ali'] + $data['bill']['money_weixin'];
        $data['bill']['refund_money'] = $data['bill']['refund_ali'] + $data['bill']['refund_weixin'];
        $data['bill']['fee_money'] = $data['bill']['fee_ali'] + $data['bill']['fee_weixin'];
        
        $map['bill_sn'] = $bill_sn;
        
        $type= I('request.type');
        $data['search']['type'] = $type;
        $date_start =I('request.date_start', 0, 'strtotime');
        $date_end =I('request.date_end', 0, 'strtotime');
        //时间筛选
        if($date_start && $date_end){
            $map['mt_order.ctime'] = array('BETWEEN', array($date_start, $date_end));
            $data['search']['date_start'] = date('Y-m-d', $date_start);
            $data['search']['date_end'] = date('Y-m-d', $date_end);
        } else if($date_start){
            $map['mt_order.ctime'] = array('EGT', $date_start);
            $data['search']['date_start'] = date('Y-m-d', $date_start);
        } else if($date_end){
            $map['mt_order.ctime'] = array('ELT', $date_end);
            $data['search']['date_end'] = date('Y-m-d', $date_end);
        }
        
        //类型筛选
        $status = I('request.status', 0, 'int');
        if ($status) {
            $map['mt_bill_order.is_refund'] = intval($status == 2);
            $data['search']['status'] = $status;
        }
        
        //支付方式筛选
        if ($type == 'weixin') {
            $map['mt_bill_order.method'] = 7;
        } else {
            $map['mt_bill_order.method'] = 6;
        }
        
        //订单号筛选
        $order_sn = I('request.order_sn', 0);
        if($order_sn){
            $map['mt_order.net_order_sn'] = array('LIKE', '%'.$order_sn.'%');
            $data['search']['order_sn'] = $order_sn;
        }
        
        //查询总数
        $count = M('bill_order')->field('mt_bill_order.bill_sn, mt_bill_order.method, mt_bill_order.is_refund, mt_bill_order.money,  mt_order.*')
        ->join('left join mt_order on mt_bill_order.net_order_sn=mt_order.net_order_sn')
        ->where($map)->count();
        //构造分页
        $listRows = isset($REQUEST['r']) ? (int)$REQUEST['r'] : C('LIST_ROWS');
        if(ceil($count/$listRows) < floatval($REQUEST['p'])){
            $_GET['p'] = ceil($count/$listRows);
        }
        $page = new \Think\Page2($count, $listRows, $REQUEST);
        $data['pager'] =$page->show();
        $limit = $page->firstRow.','.$page->listRows;
        
        $data['data_list'] = M('bill_order')->field('mt_bill_order.bill_sn, mt_bill_order.method, mt_bill_order.is_refund, mt_bill_order.money,  mt_order.*')
        ->join('left join mt_order on mt_bill_order.net_order_sn=mt_order.net_order_sn')
        ->where($map)->limit($limit)->select();

        $this->assign($data);
        $this->display();
    }
    
    /**
     * 结算操作
     */
    public function doFinish()
    {
        $bill_sn = I('request.bill_sn');
        
        $map['bill_sn'] = $bill_sn;
        $map['status'] = 0;
        $date_save['status'] = 1;
        $date_save['uptime'] = time();
        $res = M("bill")->where($map)->save($date_save);
        if ($res) {
            $msg['error'] = 0;
            $msg['info'] = "操作成功";
        } else {
            $msg['error'] = 1;
            $msg['info'] = "操作失败";
        }
        $this->ajaxReturn($msg);
    }
}